#define ProductVersion=GetEnv("ProductVersionHigh") + "." + GetEnv("ProductVersionMinor") + "." + GetEnv("ProductVersionInternal") + "." + GetEnv("ProductVersionBuild")
#define SourceDir=GetEnv("VERSIONDIR")
#define InstallDir=GetEnv("INSTALLDIR")
#define CompilerType=GetEnv("dbxext_COMPILER_TYPE")
#define CompilerVer=GetEnv("dbxext_COMPILER_VER")
#define ReleaseType=GetEnv("dbxext_RELEASE_TYPE")
#define SetupFileName=GetEnv("dbxext_SETUP_FILENAME")

#define LuxenaFullName "Luxena Software Company"
#define ProductName "dbExpress eXtension"
#define ProductDisplayName "Luxena dbExpress eXtension"
#define ProductShortName "dbxext"

#if CompilerType == "Delphi"
  #define DisplayCompilerType "Delphi"
  #if CompilerVer == "9"
    #define DisplayCompilerVer "2005"
    #define CompilerTypeForRegistry "BDS"
    #define CompilerVerForRegistry "3"
  #elif CompilerVer == "10"
    #define DisplayCompilerVer "2006"
    #define CompilerTypeForRegistry "BDS"
    #define CompilerVerForRegistry "4"
  #else
    #define DisplayCompilerVer CompilerVer
    #define CompilerTypeForRegistry "Delphi"
    #define CompilerVerForRegistry CompilerVer
  #endif
  #define CompilerSuffix ""
#else
  #define DisplayCompilerType "C++Builder"
  #define DisplayCompilerVer CompilerVer
  #define CompilerTypeForRegistry "C++Builder"
  #define CompilerVerForRegistry CompilerVer
  #define CompilerSuffix "c"
#endif


#include "L:\Products\LX_Library\Install\source\lx_library.iss"

[Setup]
AppId={#ProductName}_{#CompilerType}{#CompilerVer}_{#ReleaseType}
#if (ReleaseType == "Trial")
AppName={#ProductDisplayName} Trial
#else
AppName={#ProductDisplayName}
#endif
AppVerName={#ProductDisplayName} {#ProductVersion} {#ReleaseType} version for {#DisplayCompilerType} {#DisplayCompilerVer}
AppPublisher={#LuxenaFullName}
AppPublisherURL=http://www.luxena.com
AppSupportURL=http://www.luxena.com
AppUpdatesURL=http://www.luxena.com
DirExistsWarning=no
DisableProgramGroupPage=yes
SourceDir={#SourceDir}
OutputDir={#InstallDir}\bin
LicenseFile=license.txt
InfoBeforeFile=readme.txt
InfoAfterFile=history.txt
DefaultDirName={pf}\Luxena
OutputBaseFilename={#SetupFileName}
UninstallFilesDir="{code:AppPath}\uninstall"
Compression=none

[LangOptions]
LanguageName=English
LanguageID=$0409
WelcomeFontName=Tahoma
WelcomeFontSize=10
DialogFontName=Tahoma
DialogFontSize=8

[Components]
Name: "dbxext"; Description: "{#ProductDisplayName} {#ProductVersion}"
Name: "dbxext\bin"; Description: "Packages"; Types: full compact
Name: "dbxext\demos"; Description: "Demo Projects"; Types: full
Name: "dbxext\help"; Description: "Help Files"; Types: full compact
Name: "dbxext\lib"; Description: "Compiled Units"; Types: full compact
#if ReleaseType == "Sources"
Name: "dbxext\src"; Description: "Source Files"; Types: full
#endif
Name: "dbxext\reg"; Description: "Register in the IDE"; Types: full compact

[Files]
Source: "*.*"; DestDir: "{code:AppPath}"; Flags: ignoreversion
Components: dbxext\bin; Source: "bin\dbxext*.*"; DestDir: "{code:BinPath}"; Flags: ignoreversion
Components: dbxext\bin; Source: "bin\dcldbxext*.*"; DestDir: "{code:AppPath}\bin"; Flags: ignoreversion
Components: dbxext\demos; Source: "demos\*.*"; DestDir: "{code:AppPath}\demos"; Flags: ignoreversion recursesubdirs
Components: dbxext\help; Source: "help\*.*"; DestDir: "{code:AppPath}\help"; Flags: ignoreversion
Components: dbxext\lib; Source: "lib\*.*"; DestDir: "{code:AppPath}\lib"; Flags: ignoreversion

#if CompilerType == "CBuilder"
  Components: dbxext\lib; Source: "include\*.hpp"; DestDir: "{code:AppPath}\include"; Flags: ignoreversion
#endif
#if ReleaseType == "Sources"
  Components: dbxext\src; Source: "source\*.*"; DestDir: "{code:AppPath}\source"; Flags: ignoreversion
  Components: dbxext\src; Source: "source\packages\*{#CompilerVer}0{#CompilerSuffix}.*"; DestDir: "{code:AppPath}\source\packages"; Flags: ignoreversion
  Components: dbxext\src; Source: "source\packages\*.res"; DestDir: "{code:AppPath}\source\packages"; Flags: ignoreversion
#endif

[Registry]
Components: dbxext\reg; Root: HKCU; Subkey: "Software\Borland\{#CompilerTypeForRegistry}\{#CompilerVerForRegistry}.0\Known Packages"; ValueType: string; ValueName: "{code:AppPath}bin\dcl{#ProductShortName}{#CompilerVer}0{#CompilerSuffix}.bpl"; ValueData: "{#ProductDisplayName} {#ProductVersion}"; Flags: uninsdeletevalue
Components: dbxext\help; Root: HKLM; Subkey: Software\Microsoft\Windows\Help; ValueType: string; ValueName: {#ProductShortName}{#CompilerVer}0.hlp; ValueData: {code:AppPath}\help; Flags: uninsdeletevalue
Components: dbxext\help; Root: HKLM; Subkey: Software\Microsoft\Windows\Help; ValueType: string; ValueName: {#ProductShortName}{#CompilerVer}0.cnt; ValueData: {code:AppPath}\help; Flags: uninsdeletevalue

[_ISTool]
EnableISX=true

[Code]
function AppPath(Default: string) : string;
begin
  Result := ExpandConstant('{app}\{#ProductName}\{#CompilerType}{#DisplayCompilerVer}\');
end;

function BinPath(Default: string) : string;
begin
  Result := GetSystemDir;
  if Result = '' then
    Result := AppPath('') + 'bin';
end;

procedure RegisterDBXPathes;
var
#if CompilerType == "CBuilder"
  dbxextInclude: string;
#endif
  dbxextLibrary: string;
  dbxextSources: string;
  searchPath: string;
  browsingPath: string;
begin
#if CompilerType == "CBuilder"
  dbxextInclude := AppPath('') + 'include';
#endif
  dbxextLibrary := AppPath('') + 'lib';
  dbxextSources := AppPath('') + 'source';

  RegQueryStringValue(HKCU, 'Software\Borland\{#CompilerTypeForRegistry}\{#CompilerVerForRegistry}.0\Library', 'Search Path', searchPath);
#if CompilerType == "CBuilder"
  if Pos(dbxextInclude, searchPath) = 0 then
  begin
    if Trim(searchPath) <> '' then
      searchPath := searchPath + ';' + dbxextInclude else
      searchPath := dbxextInclude;
    RegWriteStringValue(HKCU, 'Software\Borland\{#CompilerTypeForRegistry}\{#CompilerVerForRegistry}.0\Library', 'Search Path', searchPath);
  end;
#endif
  if Pos(dbxextLibrary, searchPath) = 0 then
  begin
    if Trim(searchPath) <> '' then
      searchPath := searchPath + ';' + dbxextLibrary else
      searchPath := dbxextLibrary;
    RegWriteStringValue(HKCU, 'Software\Borland\{#CompilerTypeForRegistry}\{#CompilerVerForRegistry}.0\Library', 'Search Path', searchPath);
  end;
#if ReleaseType == "Sources"
  RegQueryStringValue(HKCU, 'Software\Borland\{#CompilerTypeForRegistry}\{#CompilerVerForRegistry}.0\Library', 'Browsing Path', browsingPath);
  if Pos(dbxextSources, browsingPath) = 0 then
  begin
    if Trim(browsingPath) <> '' then
      browsingPath := searchPath + ';' + dbxextSources else
      browsingPath := dbxextSources;
    RegWriteStringValue(HKCU, 'Software\Borland\{#CompilerTypeForRegistry}\{#CompilerVerForRegistry}.0\Library', 'Browsing Path', browsingPath);
  end;
#endif
end;

procedure UpdateHelpFile(const FileName, UpdateStr: string; Uninstall: Boolean);
var
  HelpFile: TStringList;
  Idx: Integer;
begin
  HelpFile := TStringList.Create;
  try
    HelpFile.LoadFromFile(FileName);
    Idx := HelpFile.IndexOf(UpdateStr);

    if Uninstall then
    begin
      if Idx > -1 then HelpFile.Delete(Idx);
    end else
      if Idx < 0 then HelpFile.Add(UpdateStr);

    HelpFile.SaveToFile(FileName);
  finally
    HelpFile.Free;
  end;
end;

procedure RegisterHelpFile(const CompilerHome, HelpFileName: string);
begin
  UpdateHelpFile(CompilerHome + '\Help\' + HelpFileName + '.cnt',
    ':Index {#ProductDisplayName}={#ProductShortName}{#CompilerVer}0.hlp', False);
  DeleteFile(CompilerHome + '\Help\' + HelpFileName + '.gid');
end;

procedure RegisterDBXHelp;
var
  CompilerHome: string;
begin
  #if CompilerType == "Delphi" && CompilerVer == "9"
    Exit;
  #endif

  try
    RegQueryStringValue(HKLM, 'Software\Borland\{#CompilerTypeForRegistry}\{#CompilerVer}.0', 'RootDir', CompilerHome);
    if CompilerHome <> '' then
    begin
      #if CompilerType == "Delphi" && CompilerVer == "7"
        RegisterHelpFile(CompilerHome, 'd7');
        RegisterHelpFile(CompilerHome, 'd7vcl');
      #elif CompilerType == "Delphi"
        RegisterHelpFile(CompilerHome, 'delphi{#CompilerVer}');
        RegisterHelpFile(CompilerHome, 'del{#CompilerVer}vcl');
      #elif CompilerType == "CBuilder"
        RegisterHelpFile(CompilerHome, 'bcb{#CompilerVer}');
        RegisterHelpFile(CompilerHome, 'bcb{#CompilerVer}vcl');
      #endif
    end;
  except
  end;
end;

procedure RegisterDBX;
begin
  RegisterLuxenaLibrary;

  try
    RegisterDBXPathes();
  except
  end;

  try
    RegisterDBXHelp();
  except
  end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if (CurStep = ssPostInstall) and IsComponentSelected('dbxext\reg') then
    RegisterDBX;
end;
